Stored Procedures [dbo].[asi_PareDownMailingListForPreview]
Properties
PropertyValue
ANSI Nulls OnYes
Quoted Identifier OnYes
Parameters
NameData TypeMax Length (Bytes)
@tableNamesysname256
@keyFieldNamesysname256
@startRecordint4
@recordIntervalint4
SQL Script
-- This stored procedure pares down a mailing list for previewing a Package.
-- After run, the only records remaining is the one at 'startRecord' and every
-- 'recordInterval'th record after that.  The index starts at one, so if both
-- startRecord and recordInterval are one, no records will be removed.
CREATE PROCEDURE [dbo].[asi_PareDownMailingListForPreview]
   @tableName sysname,
   @keyFieldName sysname,
   @startRecord integer,
   @recordInterval integer
AS
BEGIN
   DECLARE @sql nvarchar(800)
   DECLARE @startRecordString nvarchar(30)
   DECLARE @recordIntervalString nvarchar(30)

   CREATE TABLE #MailingListTable
   (
      MailingListTableKey uniqueidentifier,
      IntId int IDENTITY(1, 1)
   )
   
   SET @startRecordString = CAST(@startRecord AS nvarchar(30))
   SET @recordIntervalString = CAST(@recordInterval AS nvarchar(30))
       
   SET @sql = 'INSERT #MailingListTable (MailingListTableKey) SELECT [' + @keyFieldName + '] FROM [' + @tableName + ']'
   EXEC(@sql)
    
   SET @sql = 'DELETE [' + @tableName + '] FROM #MailingListTable WHERE #MailingListTable.MailingListTableKey=[' + @tableName + '].[' + @keyFieldName + '] AND (((#MailingListTable.IntId-' + @startRecordString + ')%' + @recordIntervalString + '!=0) OR #MailingListTable.IntId<' + @startRecordString + ')'
   EXEC(@sql)
END

GO
Uses